package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.query.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    //由于dataGrid组件返回必须为json,所以跳转界面就分开
    @RequestMapping("view")
    public String view(){
        return "role";
    }

    //页面数据
    @RequestMapping("query")
    @ResponseBody
    public List<Role> query(){
        return roleService.selectAll();
    }

    //员工编辑回显
    @RequestMapping("empRole")
    @ResponseBody
    public List<Long> empRole(Long id){
        return roleService.selectByEmpId(id);
    }

    //部门名称检验
    @RequestMapping("validateByName")
    @ResponseBody
    public Boolean validateByName(Long id, String name){
        return roleService.selectByName(id,name);
    }

    //编辑
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Role role){
        JSONResult result = new JSONResult();
        try{
            roleService.saveOrUpdate(role);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    //删除
    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id){
        JSONResult result = new JSONResult();
        try{
            roleService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

}
